
/***************************************************************************************************
 *
 * Project: FlashCards
 *
 * Class: HZWindowAdapter
 *
 * File name: HZWindowAdapter.java
 *
 * May 12, 2008
 *
 **************************************************************************************************/

package com.eastmountain.chinese.gui.controllers;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JFrame;
import javax.swing.Timer;

import com.eastmountain.chinese.db.exceptions.DBAccessException;
import com.eastmountain.chinese.gui.MainPanel;
import com.eastmountain.chinese.observable.HZFlashcardsDataModel;


/***************************************************************************************************
 *
 * CLASS HZWindowAdapter
 *
 **************************************************************************************************/

public class HZWindowAdapter extends WindowAdapter {

    private JFrame frame;

    public HZWindowAdapter( JFrame frame ) {

        this.frame = frame;

    }

    @Override
    public void windowClosing( WindowEvent e ) {

        displayMessage( "WindowListener method called: windowClosing." );

        // JOptionPane.showMessageDialog( frame, "Window closing invoked."
        // );

        // A pause so user can see the message before
        // the window actually closes.

        ActionListener task = new ActionListener() {

            boolean alreadyDisposed = false;

            public void actionPerformed( ActionEvent e ) {

                if ( !alreadyDisposed ) {

                    alreadyDisposed = true;

                    HZFlashcardsDataModel hzFlashcardsDataModel;

                    try {

                        hzFlashcardsDataModel = HZFlashcardsDataModel.getInstance();

                        hzFlashcardsDataModel.shutdown();

                    } catch ( DBAccessException exception ) {

                        // not much to be done here

                        exception.printStackTrace();

                    }

                    frame.dispose();

                } else { // make
                    // sure
                    // the
                    // program
                    // exits

                    System.exit( 0 );

                }

            }

        };

        Timer timer = new Timer( 500, task ); // fire
        // every
        // half
        // second
        timer.setInitialDelay( 2000 ); // first
        // delay
        // 2
        // seconds
        timer.start();

    }

    @Override
    public void windowClosed( WindowEvent e ) {

        displayMessage( "WindowListener method called: windowClosed." );

    }

    void displayMessage( String msg ) {

        System.out.println( msg + "\n" );

    }

}
